#include "emulator.hpp"
#include "game_state_pool.h"
#include <iostream>

using namespace NES;

int ram_check(NES_Byte* ram)
{
	int sum = 0;
	for (int i = 0; i < 0x800; i++)
	{
		sum += (int)ram[i];
	}
	return sum;
}

void print(int x)
{
	std::cout << x << std::endl;
}

void step(Emulator& emu, int action)
{
	NES_Byte* controller = emu.get_controller(0);
	*controller = action;
	emu.step();
}

void skip_start(Emulator& emu)
{
	step(emu, 8);
	step(emu, 0);
	step(emu, 8);
	for (int i = 0; i < 100; i++)
	{
		step(emu, 0);
	}
}

int main()
{
	std::string path = "C:/Users/pengjiawen/Downloads/gym_super_mario_bros-7.3.2/gym_super_mario_bros-7.3.2/gym_super_mario_bros/_roms/super-mario-bros.nes";

	Emulator emu(path);
	NES_Byte* ram = emu.get_memory_buffer();
	emu.reset();
	skip_start(emu);
	step(emu, 64);
	step(emu, 64);
	step(emu, 64);
	step(emu, 64);

	Emulator emu1(path);
	NES_Byte* ram1 = emu1.get_memory_buffer();

	GameStatePool pool(10);

	GameState* gs = pool.get(&emu);
	emu1.load_state(gs);

	for (int i = 0; i < 18; i++)
	{
		step(emu, 128);
		step(emu1, 128);
	}

	print(ram_check(ram));
	print(ram_check(ram1));

	return 0;
}